﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc;
using YaAppoint.Departments;
using YaAppoint.Response;

namespace YaAppoint.Controllers
{
    public class DepartmentController(IDepartmentAppService departmentAppService): AbpController
    {
        private readonly IDepartmentAppService _departmentAppService = departmentAppService;

        /// <summary>
        /// 删除
        /// </summary>
        [HttpGet("/api/department/delete")]
        public async Task<IActionResult> Delete(Guid id)
        {
            await _departmentAppService.Delete(id);
            var res = new BackJson() { code = 0 ,msg="科室已删除！"};
            return Ok(res);
        }

        [HttpGet("/api/department/get")]
        public async Task<IActionResult> GetAsync([FromQuery] Guid id)
        {
            var data = await _departmentAppService.GetAsync(id);
            var res = new BackJson()
            {
                code = 0,
                data = data
            };

            return new JsonResult(res);
        }

        /// <summary>
        /// 加载table数据
        /// </summary>
        [HttpGet("/api/department/get-list")]
        public async Task<IActionResult> GetListAsync(string? filterName)
        {
            var list = await _departmentAppService.GetListAsync(filterName);
            var res = new BackJson()
            {
                code = 0,
                data = list
            };
            return Ok(res);
        }

        /// <summary>
        /// 新建Department
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("/api/department/create")]
        public async Task<IActionResult> CreateAsync([FromBody]DepartmentCreateDto dto)
        {
            return new JsonResult(await _departmentAppService.CreateAsync(dto));
        }

        /// <summary>
        /// 更新
        /// </summary>
        [HttpPost("/api/department/update")]
        public async Task<IActionResult> UpdateAsync([FromBody] DepartmentDto dto)
        {
            return new JsonResult(await _departmentAppService.UpdateAsync(dto));
        }
    }
}
